#include "stdafx.h"
#include "GUI.h"
#include "..\Common\Viewer.h"
#include "..\..\Global\globaldef.h"
#include <time.h>

std::vector<GUI::Components::Button> GUI::Main::g_Buttons;
GUI::tStamp GUI::Main::m_MainStamp = {0,0,0,0};
D3DXVECTOR2 GUI::Main::Position = D3DXVECTOR2(0,0);
Render::tResource* GUI::Main::g_pBackgroundBar = NULL;


void GUI::Main::Update() {
	
	GUI::Main::Viewer::Update();
}

void GUI::Main::Draw() {

	GUI::Main::Viewer::Draw();
	
	RECT r;
	GUI::StampGetPixel(GUI::Main::g_pBackgroundBar, &r);

	Render::Singleton()->DrawSprite(GUI::Main::g_pBackgroundBar, Position, D3DXVECTOR2(1,1), &r);
	
	D3DXVECTOR2 p;

	Server* pServer;
	CViewer::GetServer(&pServer);

	if(pServer) {

		char stateStr[32];
		char activityStr[32];
		D3DCOLOR stateColor;
		D3DCOLOR activityColor;
		
				static int i = 0;
				i = (i > 40) ? 0 : i + 1;

		switch(pServer->GetState()) {

			case Server::SERVER_CLIENTS_CONNECTED:
				sprintf(stateStr, "Connected, Clients:%i", pServer->GetClientCount());
				stateColor = D3DCOLOR_RGBA(125, 255, 125, 255);
				float bytesIn, bytesOut;
				pServer->GetActivity(&bytesIn, &bytesOut);
				sprintf(activityStr, "Out:%.1fkb In:%.1fkb", (bytesOut / 1000.0f), (bytesIn / 1000.0f));
				if(bytesOut > 0.0f || bytesIn > 0.0f)
					activityColor = D3DCOLOR_RGBA(125, 255, 125, 255);
				else
					activityColor = D3DCOLOR_RGBA(255, 125, 125, 255);

				break;
			
			sprintf(activityStr, "No connection");
			activityColor = D3DCOLOR_RGBA(125, 125, 125, 255);

			case Server::SERVER_UNINITIALIZED:
				sprintf(stateStr, "Server Uninitalized");
				stateColor = D3DCOLOR_RGBA(255, 175, 125, 255);
				break;
			case Server::SERVER_AWAITING_CONNECTION:
				sprintf(stateStr, (i < 10 ? "Awaiting connection" :
								i < 20 ? "Awaiting connection." :
								i < 30 ? "Awaiting connection.." : 
								"Awaiting connection..."));
				stateColor = D3DCOLOR_RGBA(125, 125, 125, 255);
				break;
			default:
			case Server::SERVER_ERROR:
				sprintf(stateStr, "Server Error");
				stateColor = D3DCOLOR_RGBA(255, 125, 125, 255);
				break;

		}
		p = D3DXVECTOR2(0.51,0.0075);
		Render::Singleton()->DrawString(0,D3DCOLOR_ARGB(255,125,125,125), p, "State: [");

		p = D3DXVECTOR2(0.56,0.0075);
		Render::Singleton()->DrawString(0,stateColor, p, stateStr);
		
		p = D3DXVECTOR2(0.72,0.0075);
		Render::Singleton()->DrawString(0,D3DCOLOR_ARGB(255,125,125,125), p, "] Activity: [");

		p = D3DXVECTOR2(0.80,0.0075);
		Render::Singleton()->DrawString(0,activityColor, p, activityStr);
		
		char time[9];
		_strtime(time);
		p = D3DXVECTOR2(0.93,0.0075);
		Render::Singleton()->DrawString(0,D3DCOLOR_ARGB(255,75,75,75), p, "] %s", time);


	}
	FOREACH(GUI::Components::Button, i, g_Buttons)
		i->Draw();


}

void GUI::Main::OnMouseEvent(const float x, const float y, const GUI::MouseButton left, const GUI::MouseButton right) {

	FOREACH(GUI::Components::Button, i, g_Buttons) {
		
		i->OnMouseEvent(x,y,left,right);
	}

	GUI::Main::Viewer::OnMouseEvent(x,y,left,right);
}

bool GUI::Main::Create() {

	Render::tResource* pBackgroundBar = NULL;
	std::vector<GUI::Components::Button> buttons;

	try {

		if(!Render::Singleton()->CreateTexture("GUI\\lv_main.png", &pBackgroundBar))
			throw tException("Failed to create set texture");

		Release();

		for(uint i = 0; i < 4; i++) {

			GUI::Components::Button button = GUI::Components::Button();
			if(!button.Create( 
				i == 0 ? "Viewer" :
				i == 1 ? "Network" :
				i == 2 ? "Renderer" :
				"Help"))
				continue;

			GUI::ScaleToWindow(100 + i * 60, 0, &button.Position);
			GUI::Main::g_Buttons.push_back(button);
		}

		RECT stamp;
		stamp.left = 0;
		stamp.top = 0;
		if(!Render::Singleton()->GetTextureInfo(pBackgroundBar, (UINT*)&stamp.right, (UINT*)&stamp.bottom))
			throw tException();

		stamp.bottom /= 3;
		GUI::StampGetPercentage(&stamp, &GUI::Main::m_MainStamp);
		GUI::Main::g_pBackgroundBar = pBackgroundBar;

		GUI::Main::g_Buttons[0].OnClick = GUI::Main::Viewer::OnToggle;

		Position.x = 0;
		Position.y = 0;
		if(!GUI::Main::Viewer::Create())
			throw tException("Failed to viewer menu");
		return true;

	}catch(tException e) {

		Render::Singleton()->ReleaseResource(&pBackgroundBar);
		Release();

		std::string message;
		tException::GetMessage(&e, message);
		return false;
	}
}

bool GUI::Main::Release() {

	FOREACH(GUI::Components::Button, i, GUI::Main::g_Buttons)
		i->Release();
	GUI::Main::g_Buttons.clear();
	ZeroMemory(&GUI::Main::m_MainStamp, sizeof(RECT));
	Render::Singleton()->ReleaseResource(&GUI::Main::g_pBackgroundBar);

	GUI::Main::Viewer::Release();
	return true;
}